import hashlib
import json
import jwt
from django.forms.models import model_to_dict
from django.http import JsonResponse
from .models import User,spfltable,sptable,order
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods

from datetime import datetime


def jiemi(pwd):
    return hashlib.md5(pwd.encode()).hexdigest()
def is_empty(c):
    return c is None or str(c).strip()==""
@csrf_exempt
@require_http_methods(["POST"])
def login(request):
    data=json.loads(request.body)
    username=data["username"]
    password=data["password"]
    if is_empty(username) or is_empty(password):
        return JsonResponse({"error":"用户名或密码为空"})
    if User.objects.filter(username=username).exists()==False:
        return JsonResponse({"error":"用户名不正确"})
    usr=User.objects.get(username=username)
    if usr.password!=jiemi(password):
        return JsonResponse({"error":"密码不正确"})
    pay={"usr_username":usr.username,"usr_password":usr.password}
    mi="159357"
    token=jwt.encode(pay,mi)
    return JsonResponse({"message":"登录成功","token":token})
@require_http_methods(["GET"])
def select(request):
    all_data=spfltable.objects.all()
    list=[]
    for intem in all_data:
        list.append(model_to_dict(intem))
    return JsonResponse({"message":"查询成功","data":list})
# 商品分类(拉列表-加载商品分类表数据)筛选
@require_http_methods(["GET"])
def select1(request):
    all_data = sptable.objects.all()
    id=request.GET.get("id")
    if id:
        all_data=sptable.objects.filter(spfl_id=id)
    list = []
    for intem in all_data:
        list.append(model_to_dict(intem))
    return JsonResponse({"message": "查询成功", "data": list})
@csrf_exempt
@require_http_methods(["POST"])
def create(request):
    data=json.loads(request.body)
    jia=sptable.objects.create(
        spname=data["spname"],
        spfl_id=data["spfl_id"],
        price=data["price"],
        num=data["num"]
    )
    return JsonResponse({"message":"添加成功","data":model_to_dict(jia)})
@csrf_exempt
@require_http_methods(["PUT"])
def unique(request,id):
    pid=sptable.objects.get(id=id)
    data=json.loads(request.body)
    pid.spname=data["spname"]
    pid.spfl_id=data["spfl_id"]
    pid.price=data["price"]
    pid.num=data["num"]
    pid.create_time=datetime.now()
    pid.unique_time=datetime.now()
    pid.save()
    return JsonResponse({"messages":"修改成功"})
@csrf_exempt
@require_http_methods(["DELETE"])
def delete(request,id):
    if order.objects.filter(sp_id=id).exists()==True:
        return JsonResponse({"error":"有关联的订单删除失败"})

    sptable.objects.get(id=id).delete()
    return JsonResponse({"message":"删除成功"})
@csrf_exempt
@require_http_methods(["POST"])
def orderadd(request):
    data=json.loads(request.body)
    jia=order.objects.create(
        num=data["num"],
        sp_id=data["sp_id"],
        diprice=data["diprice"],
        kname=data["kname"],
        yongid_id=data["yongid_id"]
    )
    sp=sptable.objects.get(id=data["sp_id"])
    sp.num=int(sp.num)-(data["num"])
    sp.save()
    return JsonResponse({"message":"创建成功","data":model_to_dict(jia)})
@csrf_exempt
@require_http_methods(["GET"])
def select2(request):
    all_data=order.objects.all()
    name = request.GET.get("name")
    sptab=sptable.objects.get(spname=name)
    if name:
        all_data = all_data.filter(sp_id=sptab.id)
    list=[]

    for intem in all_data:
        list.append(model_to_dict(intem))
    return JsonResponse({"data":list,"message":"查询成功"})



